package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    final UserService userService;

    @ApiOperation("用户分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<UserVo>> pageQueryUser(@PathVariable Integer pageNum,
                                                          @PathVariable Integer pageSize,
                                                          @RequestBody UserDto userDto){
        log.info("用户分页:{},{},{}",pageNum,pageSize,userDto);

        PageBean<UserVo> pageBean = userService.pageQueryUser(pageNum,pageSize,userDto);

        return ResponseResult.success(pageBean);
    }



    @ApiOperation("用户添加")
    @PutMapping
    public ResponseResult<Void> createUser(@RequestBody UserDto userDto){
        log.info("用户添加:{}",userDto);
        userService.createUser(userDto);
        return ResponseResult.success();
    }





    @ApiOperation("用户修改")
    @PatchMapping
    public ResponseResult<Void> updateUser(@RequestBody UserDto userDto){
        log.info("用户修改:{}", userDto);
        userService.updateUser(userDto);
        return ResponseResult.success();
    }



    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{userIds}")
    public ResponseResult<Void> deleteUser(@PathVariable String userIds){
        log.info("删除用户:{}", userIds);
        userService.deleteUser(userIds);
        return ResponseResult.success();
    }




    @ApiOperation("启用或禁用用户")
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult<Void> enableOrDisableUser(@PathVariable Long id,
                                                    @PathVariable String status){
        log.info("启用或禁用用户：{},{}", id, status);
        userService.enableOrDisableUser(id, status);
        return ResponseResult.success();
    }





    @ApiOperation("用户列表")
    @PostMapping("/list")
    public ResponseResult<List<UserVo>> userList(@RequestBody UserDto userDto){
        log.info("用户列表：{}", userDto);
        List<UserVo> userVos = userService.findUserList(userDto);
        return ResponseResult.success(userVos);
    }



    @ApiOperation("密码重置")
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult<Void> resetPasswords(@PathVariable String userId){
        log.info("密码重置：{}", userId);
        userService.resetPassword(userId);
        return ResponseResult.success();
    }



    @ApiOperation("用户个人数据回显")
    @GetMapping("/current-user")
    public ResponseResult<UserVo> getUserInfo(){
        log.info("获取用户个人数据");
        UserVo userVo = userService.getUserInfo();
        return ResponseResult.success(userVo);
    }


//    @ApiOperation("用户查询自己密码")
//    @PutMapping("cmodify-passwords")
//    public ResponseResult<Void> modifyPasswords(@RequestBody UserDto userDto){
//        log.info("修改密码：{}", userDto);
//        userService.modifyPasswords(userDto);
//        return ResponseResult.success();
//    }
}
